/**
 * AlertDefinitionAction.java
 * Created at 2017-2-8
 * Created by Administrator
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.rms.system.model.TtAlertDefinition;
import com.broadtext.rms.system.service.IAlertDefinitionService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>
 * ClassName: AlertDefinitionAction
 * </p>
 * <p>
 * Description: 告警定义管理控制层
 * </p>
 * <p>
 * Author: shihui
 * </p>
 * <p>
 * Date: 2017-4-25
 * </p>
 */
@Controller
@RequestMapping("/main/alertDefinition")
public class AlertDefinitionAction {
    /**
     * 日志信息
     */
    public static Logger logger = LoggerFactory.getLogger(AlertDefinitionAction.class);

    /**
     * 区域服务类
     */
    @Autowired
    @Qualifier("alertDefinitionService")
    private IAlertDefinitionService alertDefinitionService;
    
    /**
     * 
     * <p>Description: 走查   区域分页查询  修改方法的对应描述，总电压  总电流   单体最高温度   单体温度与温差   单体电压与压差  </p>
     * @param request 请求对象
     * @param response 返回对象
     * @return 查询结果数据
     * @throws ServiceException 异常
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/findEntityPageByQuery.mvc")
    @ResponseBody
    public void findEntityPageByQuery(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        String jsonData;
        jsonData = request.getParameter("aoData");
        JSONArray array;
        array = JSON.parseArray(jsonData);
        PageBounds pageBounds;
        pageBounds = new PageBounds();
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        if (array != null) {
            Iterator it;
            it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && !"".equals(obj.get("value") + "")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        EntityPageBean<TtAlertDefinition> pageBean =  this.alertDefinitionService.findEntityPageByQuery(map, pageBounds);
        Map<String, Object> dataMap=new HashMap<String, Object>();
        dataMap.put("recordsTotal", pageBean.getiTotalRecords());
        dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
        dataMap.put("aaData", pageBean.getResults());
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(JSON.toJSONString(dataMap));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/findAlertLevelOpt.mvc")
    @ResponseBody
    public Map<String, Object> getAlertLevelOpt (HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        List<Object> list;
        try {
            list = this.alertDefinitionService.getAlertLevelOpt();
            map.put("dict", list);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
}
